<?php
/**
 * 二手房
 * User: E 房cms www.fangcms.com
 * Date: 2015/1/20
 * Time: 10:51
 */
namespace Wap\Controller;
use Org\Yike\FangCms;
class UsedController extends CommonController{
    public function _initialize(){
        parent::_initialize();
        $this->is_login ();
        $this->action = strtolower(ACTION_NAME);
        $this->title='房源';
    }

    public function index (){
        $type = I ('get.type', 0);
        if (!empty ($type)){
            switch ($type){
                case 1:
                    $map['us.total_price'] = array ('lt', 100);
                    break;
                case 2:
                    $map['us.total_price'] = array ('between', '100,150');
                    break;
                case 3:
                    $map['us.total_price'] = array ('between', '150,200');
                    break;
                case 4:
                    $map['us.total_price'] = array ('between', '200,250');
                    break;
                case 5:
                    $map['us.total_price'] = array ('between', '250,300');
                    break;
                case 7:
                    $map['us.chamber'] = array ('eq', 1);
                    break;
                case 8:
                    $map['us.chamber'] = array ('eq', 2);
                    break;
                case 9:
                    $map['us.chamber'] = array ('eq', 3);
                    break;
                case 10:
                    $map['us.chamber'] = array ('eq', 4);
                    break;
                case 11:
                    $map['us.chamber'] = array ('eq', 5);
                    break;
                case 12:
                    $map['us.chamber'] = array ('gt', 5);
                    break;
                case 14:
                    $map['us.user_id'] = $this->user['id'];
                    break;
                case 15:
                    $map['us.owner'] = 2;
                    break;
                default :
                    $map = array ('us.chamber' => $type);
                    break;
            }
        }
        if (IS_POST){
            $min = I ('post.price_min', 0);
            $max = I ('post.price_max', '');
            if ((!empty ($max)) && (!empty ($min))){
                $map['us.total_price'] = array ('between', $min.','.$max);
            }
        }
        //房源列表
        $map['_string'] = 'us.status<>4';
        $table = array ('yk_used'=>'us');
        $field = 'us.*,u.username,u.mobile';

        $houses = M ('used')->field ($field)->table($table)
            ->join ('yk_user u on us.user_id=u.id', 'LEFT')
            ->join ('yk_room r on us.dict_id=r.rid', 'LEFT')
            ->where ($map)
            ->limit (0,10)->select ();

        $this->assign ('page', 1);
        $this->assign ('map', json_encode($map));
        $this->assign ('houses', $houses);
        $this->display();
    }
    public function loading (){
        $json_map = I ('post.map', '');
        $ori_page = I ('post.page', '');
        $map = json_decode ($json_map);
        $page = $ori_page*10;
        $field = 'us.*,u.username,u.mobile';
        $table = array ('yk_used'=>'us');
        $houses = M ('used')->field ($field)->table ($table)
            ->join ('yk_user u on us.user_id=u.id', 'LEFT')
            ->join ('yk_room r on us.dict_id=r.rid', 'LEFT')
            ->where ($map)
            ->limit ($page,10)
            ->select ();
        if ($houses){
            exit (json_encode(array ('state'=>'ok','data'=>$houses,'map'=>$json_map,'page'=>$ori_page+1)));
        }
        exit (json_encode(array ('state'=>'0','data'=>'','map'=>$json_map,'page'=>$ori_page+1)));

    }

    //楼盘主页
    public function detail(){
        $id = I ('get.id', '');
        $detail = M ('used')->where ('id='.$id)->find ();
        $detail['file'] = json_decode($detail['file']);
        $detail['tags'] = explode(',', $detail['tags']);
        $this->assign ('detail', $detail);
        $this->display();
    }
    public function add (){
        if (IS_POST){
            $data = I ('post.');
            //是否已存在房源
            $is_exist = M ('used')->where (array ('dict_id'=>$data['room']))->getField('id');
            if (!empty ($is_exist)){
                $this->error ('房源已存在',U('add'));
            }
            $insert_data = array (
                'title'=>$data['title'],
                'dict_id'=>$data['room'],
                'sell_type'=>$data['sell_type'],
                'address'=>$data['address'],
                'estate_id' => $data['estate_id'],
                'estate' => $data['estate'],
                'area_id' => $data['area_id'],
                'used_id'=>$this->user['id']);
            $res = M ('used')->add ($insert_data);
            if ($res){
                $this->redirect('Used/basic', array('id'=>$res), 0);
            }else{
                $this->error('出错！');
            }
        }
        $this->display ();
    }
    public function basic (){
        $used = M ('used')->where ('id='.I ('get.id',''))->find ();
        $model = M ('room');
        $model->table (array ('yk_room'=>'r'));
        $model->field ('r.*,un.unit,un.alias as unalias,b.build,b.alias as balias');
        $model->join ('yk_unit un on r.unit=un.unid', 'LEFT');
        $model->join ('yk_build b on r.build=b.bid', 'LEFT');
        $model->where ('rid='.$used['dict_id']);
        $dict = $model->find ();
        //小区信息
        $estate = M ('estate')->field ('id,title,years,map')->where ('id='.$used['area_id'])->find ();

        //var_dump($estate);exit;
        $this->assign ('used', $used);
        $this->assign ('dict', $dict);
        $this->assign ('estate', $estate);
        $this->display();
    }
    public function save (){
        $data = I ('post.', '');
        $used_type = explode('-', $data['used_type']);
        $data['chamber'] = $used_type[0];
        $data['hall'] = $used_type[1];
        $data['kitchen'] = $used_type[2];
        $data['toilet'] = $used_type[3];
        $data['add_time'] = date ('Y-m-d H:i:s');
        $res = M ('used')->save ($data);
        if ($res === false){
            $this->error ('录入失败');
        }else{
            $this->display ('finish');
        }
    }
    //跟进房源
    public function follow (){
        if(IS_POST){
            $user_id = session('userInfo.id');
            $used_id = I ('post.usedid', '');
            $conter = I ('post.conter', '');
            $uname = M ('user')->where (array ('id'=>$user_id))->getField('username');
            $today = date ('Y-m-d');
            $starTime = strtotime ($today." 00:00:00");
            $endTime = strtotime ($today." 23:59:59");
            $follow  = M ('follow')
                ->where (array ('user_id'=>$user_id,'source_id'=>$used_id,'discuss'=>NULL,'add_time' =>array ('between', $starTime.','.$endTime+1)))
                ->find ();
            if (empty ($follow)){
                $data = array (
                    'user_id' => $user_id,
                    'source_id' => $used_id,
                    'type' => 1,
                    'discuss' => $conter,
                    'uname' => $uname,
                    'add_time' => time()

                );
                $res = M ('follow')->add($data);
            }else{
                $res = M ('follow')
                    ->where (array ('user_id'=>$user_id,'source_id'=>$used_id,'discuss'=>NULL,'add_time' =>array ('between', $starTime.','.$endTime+1)))
                    ->setField('discuss',$conter);
            }
            if ($res){
                $this->success('跟进成功', U('used/index'));
            }else{
                $this->error ('网络出错');
            }
        }
        $this->assign ('usedid', I ('get.used_id'));
        $this->display ();
    }
}